<?php
/**
 * Static content controller.
 *
 * This file will render views from views/pages/
 *
 * PHP 5
 *
 * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
 * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
 *
 * Licensed under The MIT License
 * For full copyright and license information, please see the LICENSE.txt
 * Redistributions of files must retain the above copyright notice.
 *
 * @copyright     Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
 * @link          http://cakephp.org CakePHP(tm) Project
 * @package       app.Controller
 * @since         CakePHP(tm) v 0.2.9
 * @license       MIT License (http://www.opensource.org/licenses/mit-license.php)
 */
App::uses('AppController', 'Controller');

/**
 * Static content controller
 *
 * Override this controller by placing a copy in controllers directory of an application
 *
 * @package       app.Controller
 * @link http://book.cakephp.org/2.0/en/controllers/pages-controller.html
 */
class ClientsController extends AppController {
	
	public $uses = array('Client','User');
	
	public $helpers = array('Html', 'Form');
	
	public function beforeFilter() {
		parent::beforeFilter();
		if ($this->Session->read('Auth.User.role') === 'superadmin' || $this->Session->read('Auth.User.role') === 'managers') {
			return true;
		}else{
			$this->redirect(array('controller' => 'timesheets', 'action' => 'index'));
		}
	}
	
	public function isAuthorized($user) {
		// All registered users can add posts
		if ($this->action === 'add') {
			return true;
		}
	
		// The owner of a post can edit and delete it
		if (in_array($this->action, array('index', 'edit', 'delete'))) {
			return false;
			$postId = $this->request->params['pass'][0];
			if ($this->Post->isOwnedBy($postId, $user['id'])) {
				
			}
		}
	
		return parent::isAuthorized($user);
	}
	
	public $paginate = array(
        'limit' => 20,
        'order' => array(
            'Client.title' => 'asc')
    );
	
	public function index(){
		$this->set('clients', $this->paginate()); 
			
		$this->set('users', $this->User->find('all'));
		
	}
	
	public function view($id = null) {
        if (!$id) {
            throw new NotFoundException(__('Invalid client'));
        }

        $client = $this->Client->findById($id);
        if (!$client) {
            throw new NotFoundException(__('Invalid client'));
        }
        $this->set('client', $client);
    }
	
	public function add() {
		if ($this->request->is('post')) {
			$this->request->data['Client']['created_by'] = $this->Auth->user('id'); //Added this line
			if ($this->Client->save($this->request->data)) {
				$this->Session->setFlash('Your client has been saved.');
				$this->redirect(array('action' => 'index'));
			}
		}
	}
	
	public function edit($id = null) {
		if (!$id) {
			throw new NotFoundException(__('Invalid client'));
		}
	
		$client = $this->Client->findById($id);
		if (!$client) {
			throw new NotFoundException(__('Invalid client'));
		}
	
		if ($this->request->is('client') || $this->request->is('put')) {
			$this->Client->id = $id;
			$this->request->data['Client']['modified_by'] = $this->Auth->user('id'); //Added this line
			if ($this->Client->save($this->request->data)) {
				$this->Session->setFlash('Your client has been updated.');
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash('Unable to update your client.');
			}
		}
	
		if (!$this->request->data) {
			$this->request->data = $client;
		}
	}
	
	public function delete($id) {
		if ($this->request->is('get')) {
			throw new MethodNotAllowedException();
		}
	
		if ($this->Client->delete($id)) {
			$this->Session->setFlash('The client with id: ' . $id . ' has been deleted.');
			$this->redirect(array('action' => 'index'));
		}
	}
}
